//
//  KYHeightView.m
//  个人信息修改
//
//  Created by Yip-Jun on 16/5/4.
//  Copyright © 2016年 YIPWJ. All rights reserved.
//

#import "YZCoverView.h"
#import "Masonry.h"
#import "YZSaveDataTool.h"
#import "YZBodyPickerView.h"
#import "YZProvinceTableView.h"
#import "YZProvinces.h"
#import "YZProvinceTableViewCell.h"

@interface YZCoverView () <UIPickerViewDataSource, UIPickerViewDelegate, UITableViewDelegate, UITableViewDataSource>

// 弹窗
@property (nonatomic, strong) UIView *heightView;
// 文本
@property (nonatomic, strong) UILabel *titleLabel;
// 提交
@property (nonatomic, strong) UIButton *pushButton;
// 取消
@property (nonatomic, strong) UIButton *cannelButton;
// bodyPickerView
@property (nonatomic, strong) YZBodyPickerView *bodyPickerView;
// provincePickerView
@property (nonatomic, strong) YZProvinceTableView *provinceTableView;
// 横分割线
@property (nonatomic, strong) UIView *lineV;
// 竖分割线
@property (nonatomic, strong) UIView *lineH;
// pickerView 显示的数据内容
@property (nonatomic, strong) NSArray *pickerViewArr;
// 用于存储 pickerView 选中的内容
@property (nonatomic, strong) NSMutableArray *pickerString;
// 获取 pickerString 数组里面的
@property (nonatomic, copy) NSString *titleString;

@property (nonatomic, strong) NSArray *provinces;
// 记录 lineV 的旧约束
@property (nonatomic, strong) MASConstraint *oldConstraint;
// 记录 heightView 的旧约束
@property (nonatomic, strong) MASConstraint *heightOldConstraint;
// 记录点钟的 cell
@property (nonatomic, strong) NSIndexPath * selectIndexPath;
// 记录被选中的省份名字
@property (nonatomic, strong) NSString *selectName;

@end

@implementation YZCoverView

+ (instancetype)coverView: (NSIndexPath *)coverIndexpath {
    YZCoverView *coverView = [[YZCoverView alloc] init];
    
//    [coverView setIndexPath:coverView.indexPath];
    [coverView prepareUI: coverIndexpath];
    [coverView setFrameOfSubView: coverIndexpath];

    return coverView;
}

- (void)setIndexPath:(NSIndexPath *)indexPath {
    _indexPath = indexPath;

    if (indexPath.row == 0) {
        self.titleLabel.text = @"设置身高（cm）";
    } else if (indexPath.row == 1) {
        self.titleLabel.text = @"设置体重（kg）";
    } else {
        self.titleLabel.text = @"设置省份";
    }
    [self updataConstraint:indexPath];
}

- (void)updataConstraint: (NSIndexPath *)indexPath {
    [self.oldConstraint uninstall];
    UIView *view = [[UIView alloc] init];
    if (indexPath.row == 2) {
        [self.heightView addSubview:self.provinceTableView];
        view = (UIView *)self.provinceTableView;
        CGFloat height = [UIScreen mainScreen].bounds.size.width * 0.8;
        [view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.titleLabel.mas_bottom);
            make.leading.equalTo(self.heightView).offset(10);
            make.trailing.equalTo(self.heightView).offset(-10);
            make.height.mas_equalTo(height);
        }];
    } else {
        [self.heightView addSubview:self.bodyPickerView];
        view = (UIView *)self.bodyPickerView;
        [view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.titleLabel.mas_bottom);
            make.leading.equalTo(self.heightView).offset(10);
            make.trailing.equalTo(self.heightView).offset(-10);
            CGFloat heightViewH = [UIScreen mainScreen].bounds.size.height * 0.2;
            make.height.mas_equalTo(heightViewH);
        }];
    }
    
    [self.lineV mas_makeConstraints:^(MASConstraintMaker *make) {
        self.oldConstraint = make.top.equalTo(view.mas_bottom).offset(16);
    }];

}

- (void)prepareUI: (NSIndexPath *)coverIndexPath {

    // 添加控件
    [self addSubview:self.heightView];
    [self.heightView addSubview:self.titleLabel];
    [self.heightView addSubview:self.pushButton];
    [self.heightView addSubview:self.cannelButton];
    [self.heightView addSubview:self.lineV];
    [self.heightView addSubview:self.lineH];
}

- (void)setFrameOfSubView: (NSIndexPath *)coverIndexpath {
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.heightView);
        make.top.equalTo(self.heightView).offset(10);
    }];
 
    [self.lineV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self.heightView.mas_width);
        make.height.mas_equalTo(1);
        self.oldConstraint = make.top.equalTo(self.titleLabel.mas_bottom).offset(16);
        make.centerX.equalTo(self);
    }];

    [self.cannelButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.lineV).offset(6);
        make.trailing.equalTo(self.mas_centerX).offset(-20);
        make.width.mas_equalTo(100);
        make.height.mas_equalTo(40);
    }];
    
    [self.pushButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.cannelButton);
        make.width.equalTo(self.cannelButton);
        make.height.equalTo(self.cannelButton);
        make.leading.equalTo(self.mas_centerX).offset(20);
    }];
    
    [self.lineH mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.height.equalTo(self.cannelButton.mas_height);
        make.width.mas_equalTo(1);
        make.top.equalTo(self.cannelButton);
    }];
    
    CGFloat heightViewW = [UIScreen mainScreen].bounds.size.width * 0.8;
    CGFloat heightViewH = heightViewW * 0.7;
    CGFloat heightViewX = [UIScreen mainScreen].bounds.size.width * 0.1;
    CGFloat heightViewY = coverIndexpath.row == 2 ? [UIScreen mainScreen].bounds.size.height * 0.2 :[UIScreen mainScreen].bounds.size.height * 0.45;
    [self.heightView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self).offset(heightViewX);
        make.top.equalTo(self).offset(heightViewY);
        make.bottom.equalTo(self.pushButton).offset(6);
//        self.heightOldConstraint = make.height.mas_equalTo(heightViewH);
        make.width.mas_equalTo(heightViewW);
    }];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    self.pushButton.userInteractionEnabled = NO;
    self.cannelButton.userInteractionEnabled = NO;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    self.pushButton.userInteractionEnabled = YES;
    self.cannelButton.userInteractionEnabled = YES;
}

- (void)pushButtonDidClick {
    
    NSMutableString *string = [NSMutableString string];
//    for (NSString *str in self.pickerString) {
//        [string appendString:str];
//    }

    if (![self.message isEqualToString:@"省份"]) {
        
        for (NSString *str in self.pickerString) {
            [string appendString:str];
        }
    } else {
        [string appendString:self.selectName];
    }
    
    [YZSaveDataTool setValue:string forKey:self.message];
    
    self.pushBtnDidClickBlock(string);
}

- (void)cannelButtonDidClick {
    self.cannelBtnDidClickBlock();
}

#pragma mark - pickerView 数据源方法
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return self.pickerViewArr.count;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {

    return [self.pickerViewArr[component] count];

}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {

    UILabel *label = [[UILabel alloc] init];

    NSArray *arr = self.pickerViewArr[component];
    NSString *str = arr[row];
    
    label.text = str;
    label.font = [UIFont systemFontOfSize:18];
    label.textAlignment = NSTextAlignmentCenter;
    
    return label;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    NSString *str = [NSString string];
    if (component == 0) {
        str = self.pickerViewArr[component][row];
        self.pickerString[0] = str;
    } else if (component == 1) {
        str = self.pickerViewArr[component][row];
        self.pickerString[1] = str;
    } else if (component == 2) {
        str = self.pickerViewArr[component][row];
        self.pickerString[2] = str;
    }
}

#pragma mark - tableView 代理方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.provinces.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    YZProvinceTableViewCell *cell = [YZProvinceTableViewCell provinceTableViewCell];
    
    YZProvinces *province = self.provinces[indexPath.row];
    cell.province = province;
    if (self.selectName == cell.province.name) {
        cell.isSelect = [YZSaveDataTool boolForKey:cell.province.name];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        YZProvinceTableViewCell *cell = [[YZProvinceTableViewCell alloc] init];
        if (self.selectIndexPath) {
            cell = [tableView cellForRowAtIndexPath:self.selectIndexPath];
            cell.isSelect = !cell.isSelect;
            [tableView reloadRowsAtIndexPaths:@[self.selectIndexPath] withRowAnimation:UITableViewRowAnimationNone];
        }
        self.selectIndexPath = indexPath;
        cell = [tableView cellForRowAtIndexPath:indexPath];
        self.selectName = cell.province.name;
        cell.isSelect = !cell.isSelect;
}



#pragma mark - MARK - 懒加载
- (UIView *)heightView {
    if (!_heightView) {
        _heightView = [[UIView alloc] init];
        _heightView.backgroundColor = [UIColor whiteColor];
        _heightView.layer.cornerRadius = 15;
    }
    return _heightView;
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.text = @"设置身高(cm)";
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.font = [UIFont systemFontOfSize:16];
        _titleLabel.textColor = [UIColor colorWithRed:((40) / 255.0) green:((200) / 255.0) blue:((200) / 255.0) alpha:1.0];
    }
    return _titleLabel;
}

- (YZBodyPickerView *)bodyPickerView {
    if (!_bodyPickerView) {
        _bodyPickerView = [[YZBodyPickerView alloc] init];
        _bodyPickerView.dataSource = self;
        _bodyPickerView.delegate = self;
    }
    return _bodyPickerView;
}

- (YZProvinceTableView *)provinceTableView {
    if (!_provinceTableView) {
        _provinceTableView = [[YZProvinceTableView alloc] init];
        _provinceTableView.dataSource = self;
        _provinceTableView.delegate = self;
    }
    return _provinceTableView;
}

- (UIButton *)pushButton {
    if (!_pushButton) {
        _pushButton = [[UIButton alloc] init];
//        _pushButton.userInteractionEnabled = YES;
        [_pushButton setTitle:@"提交" forState:UIControlStateNormal];
        [_pushButton addTarget:self action:@selector(pushButtonDidClick) forControlEvents:UIControlEventTouchUpInside];
        [_pushButton setTitleColor:[UIColor colorWithRed:((40) / 255.0) green:((200) / 255.0) blue:((200) / 255.0) alpha:1.0] forState:UIControlStateNormal];
    }
    return _pushButton;
}

- (UIButton *)cannelButton {
    if (!_cannelButton) {
        _cannelButton = [[UIButton alloc] init];
//        _cannelButton.userInteractionEnabled = YES;
        [_cannelButton setTitle:@"取消" forState:UIControlStateNormal];
        [_cannelButton addTarget:self action:@selector(cannelButtonDidClick) forControlEvents:UIControlEventTouchUpInside];
        [_cannelButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    }
    return _cannelButton;
}

- (UIView *)lineV {
    if (!_lineV) {
        _lineV = [[UIView alloc] init];
        _lineV.backgroundColor = [UIColor colorWithRed:((170) / 255.0) green:((170) / 255.0) blue:((170) / 255.0) alpha:1.0];;
    }
    return _lineV;
}

- (UIView *)lineH {
    if (!_lineH) {
        _lineH = [[UIView alloc] init];
        _lineH.backgroundColor = [UIColor colorWithRed:((170) / 255.0) green:((170) / 255.0) blue:((170) / 255.0) alpha:1.0];;
    }
    return _lineH;
}

- (NSArray *)pickerViewArr {
    if (!_pickerViewArr) {
        NSArray *arr1 = [NSArray arrayWithObjects:@"0", @"1", @"2", nil];
        NSArray *arr2 = [NSArray arrayWithObjects:@"0", @"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9", nil];
        NSArray *arr3 = [NSArray arrayWithObjects:@"0", @"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9", nil];
        _pickerViewArr = [NSArray arrayWithObjects:arr1, arr2, arr3, nil];
    }
    return _pickerViewArr;
}

- (NSMutableArray *)pickerString {
    if (!_pickerString) {
        _pickerString = [NSMutableArray arrayWithObjects:@"0", @"0", @"0", nil];
    }
    return _pickerString;
}

- (NSArray *)provinces {
    if (!_provinces) {
        _provinces = [YZProvinces provinces];
    }
    return _provinces;
}

- (NSString *)selectName {
    if (!_selectName) {
        _selectName = [NSString string];
    }
    return _selectName;
}

@end
